package client.comm.octfdsmall.widget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import androidx.viewpager.widget.ViewPager


class CustomViewPager1 : ViewPager {
    private var current:Int = 0
     var height:Int? = 0

    /**
     * 保存position与对于的View
     */
    private val mChildrenViews: HashMap<Int, View> = LinkedHashMap<Int, View>()
    var isScrollble = true

    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var heightMeasureSpec = heightMeasureSpec
        if (mChildrenViews.size > current) {
            val child: View? = mChildrenViews[current]
            if (child != null) {
                child.measure(
                    widthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
                )
                height = child.getMeasuredHeight()
            }
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height!!, MeasureSpec.EXACTLY)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    fun resetHeight(current: Int) {
        this.current = current
        if (mChildrenViews.size > current) {
            var layoutParams = layoutParams as LinearLayout.LayoutParams
            if (layoutParams == null) {
                layoutParams =
                    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, height!!)
            } else {
                layoutParams.height = height!!
            }
            setLayoutParams(layoutParams)
        }
    }

    /**
     * 保存position与对于的View
     */
    fun setObjectForPosition(view: View, position: Int) {
        mChildrenViews[position] = view
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        return if (!isScrollble) {
            true
        } else super.onTouchEvent(ev)
    }


}